using System.Collections.ObjectModel;
using FileTransfer.Client;

namespace FileTransferDesktop.ViewModels.Dialogs;

public partial class MessageEditDialogViewModel(ApiClient apiClient) : ObservableObject
{
    [ObservableProperty]
    private string _shareCode = string.Empty;

    [ObservableProperty]
    private string _editToken = string.Empty;

    [ObservableProperty]
    private ObservableCollection<FileTransfer.Client.Http.Model.Message?> _messages = [];

    [ObservableProperty]
    private string _newMessageContent = string.Empty;

    [ObservableProperty]
    private bool _isLoading = false;

    [ObservableProperty]
    private string _errorMessage = string.Empty;

    [ObservableProperty]
    private bool _hasError = false;

    public Action? RequestClose { get; set; }

    public async Task LoadMessagesAsync(string shareCode, string editToken)
    {
        ShareCode = shareCode;
        EditToken = editToken;

        if (string.IsNullOrEmpty(shareCode))
            return;

        IsLoading = true;
        HasError = false;
        ErrorMessage = string.Empty;

        try
        {
            var messages = await apiClient.MessageControllerApi.GetMessagesAsync(shareCode);
            Messages.Clear();
            foreach (var message in messages.OrderBy(m => m.CreatedAt))
            {
                Messages.Add(message);
            }
        }
        catch (Exception ex)
        {
            HasError = true;
            ErrorMessage = $"加载消息失败: {ex.Message}";
        }
        finally
        {
            IsLoading = false;
        }
    }

    [RelayCommand]
    private async Task AddMessageAsync()
    {
        if (
            string.IsNullOrWhiteSpace(NewMessageContent)
            || string.IsNullOrEmpty(ShareCode)
            || string.IsNullOrEmpty(EditToken)
        )
            return;

        IsLoading = true;
        HasError = false;
        ErrorMessage = string.Empty;

        try
        {
            var newMessage = await apiClient.MessageControllerApi.AddMessageAsync(
                ShareCode,
                EditToken,
                NewMessageContent.Trim()
            );

            Messages.Add(newMessage);
            NewMessageContent = string.Empty;
        }
        catch (Exception ex)
        {
            HasError = true;
            ErrorMessage = $"添加消息失败: {ex.Message}";
        }
        finally
        {
            IsLoading = false;
        }
    }

    [RelayCommand]
    private async Task DeleteMessageAsync(FileTransfer.Client.Http.Model.Message? message)
    {
        if (message == null || string.IsNullOrEmpty(ShareCode) || string.IsNullOrEmpty(EditToken))
            return;

        IsLoading = true;
        HasError = false;
        ErrorMessage = string.Empty;

        try
        {
            var success = await apiClient.MessageControllerApi.DeleteMessageAsync(
                ShareCode,
                EditToken,
                message.Id
            );

            if (success)
            {
                Messages.Remove(message);
            }
            else
            {
                HasError = true;
                ErrorMessage = "删除消息失败";
            }
        }
        catch (Exception ex)
        {
            HasError = true;
            ErrorMessage = $"删除消息失败: {ex.Message}";
        }
        finally
        {
            IsLoading = false;
        }
    }

    [RelayCommand]
    private void Close()
    {
        RequestClose?.Invoke();
    }

    [RelayCommand]
    private void ClearError()
    {
        HasError = false;
        ErrorMessage = string.Empty;
    }
}
